/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yangxu.hospital.system.modules.system.service.impl;

import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yangxu.hospital.common.exception.BadRequestException;
import com.yangxu.hospital.common.utils.FileUtil;
import com.yangxu.hospital.common.utils.PageResult;
import com.yangxu.hospital.common.utils.PageUtil;
import com.yangxu.hospital.common.utils.RedisUtils;
import com.yangxu.hospital.system.modules.system.domain.Role;
import com.yangxu.hospital.system.modules.system.domain.SysPatient;
import com.yangxu.hospital.system.modules.system.domain.User;
import com.yangxu.hospital.system.modules.system.domain.dto.SysPatientDto;
import com.yangxu.hospital.system.modules.system.mapper.UserMapper;
import com.yangxu.hospital.system.modules.system.mapper.UserRoleMapper;
import com.yangxu.hospital.system.modules.system.service.UserService;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yangxu.hospital.system.modules.system.service.SysPatientService;
import com.yangxu.hospital.system.modules.system.domain.vo.SysPatientQueryCriteria;
import com.yangxu.hospital.system.modules.system.mapper.SysPatientMapper;
import net.dreamlu.mica.core.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @description 服务实现
* @author 杨旭
* @date 2024-11-25
**/
@Service
@RequiredArgsConstructor
public class SysPatientServiceImpl extends ServiceImpl<SysPatientMapper, SysPatient> implements SysPatientService {

    private final RedisUtils redisUtils;

    private final SysPatientMapper sysPatientMapper;


    @Override
    public PageResult<SysPatientDto> queryAll(SysPatientQueryCriteria criteria, Page<Object> page){
        IPage<SysPatientDto> all = sysPatientMapper.findAll(criteria, page);
        return PageUtil.toPage(all);
    }

    @Override
    public List<SysPatient> queryAll(SysPatientQueryCriteria criteria){
        return sysPatientMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SysPatient resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysPatient resources) {
        SysPatient sysPatient = getById(resources.getPatientId());
        sysPatient.copy(resources);
        saveOrUpdate(sysPatient);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<SysPatient> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysPatient sysPatient : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("过敏信息", sysPatient.getAllergies());
            map.put("紧急联系人信息", sysPatient.getEmergencyContact());
            map.put("创建时间", sysPatient.getCreateTime());
            map.put("更新时间", sysPatient.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}